// 工具函数

// 返回参数
const setParam = (ctx, status, msg, results = undefined, ...arg) => {
    return ctx.body = {
        status,
        msg,
        results,
        ...arg[0]
    }
}

// 去除空格
const delTrim = (data) => {
    return data.trim()
}

// 判断是字符串
const isString = (data) => {
    if(typeof data === 'string'){
        return true
    }
    return false
}

// 非空校验
const isEmpty = (data) => {
    if(data === '' || data === null || data === undefined || data === NaN) {
        return false
    }
    return true
}

/**
 * num:
 * 0 -> 当前时间戳
 * 1 -> xxxx-xx-xx xx:xx:xx
 * 2 -> xxxx-xx-xx
 * 3 -> xxxxxxxx
*/
const nowDate = (num = 0) => {
    let date = new Date()
    let y = date.getFullYear()
    let m = date.getMonth() + 1

    m = m < 10 ? `0${m}` : m

    let d = date.getDate()
    let h = date.getHours()
    let mm = date.getMinutes()
    let s = date.getSeconds()

    s = s < 10 ? `0${s}` : s

    switch (num) {
        case 1: {
            return `${ y }-${ m }-${ d } ${ h }:${ mm }:${ s }`
        }

        case 2: {
            return `${ y }-${ m }-${ d }`
        }

        case 3: {
            return `${ y }${ m }${ d }`
        }
    
        default: {
            return date.getTime()
        }
    }
}


module.exports = {
    delTrim, 
    isString, 
    isEmpty, 
    nowDate,
    setParam
}
